package allinpay.yun.demo;

import com.fasterxml.jackson.databind.ObjectMapper;
import ime.service.client.SOAClient;
import ime.service.util.RSAUtil;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.net.URL;
import java.security.PrivateKey;
import java.security.PublicKey;

public class DemoPortal {
    //商户号
    static String sysid = "100009001000";
    //密钥密码
    static String pwd = "900724";
    //证书名称
    static String alias = "100009001000";
    //证书文件路径，用户指定
    static String path = "cert/100009001000.pfx";
    //加密方法
    static String signMethod = "SHA1WithRSA";
    //SOA接口地址
    static String serverAddress = "http://116.228.64.55:9092/service/soa";
    //SDK
    private static SOAClient client;
    //公钥
    private static PublicKey publicKey;
    //私钥
    private static PrivateKey privateKey;
    //每个接口handler的工厂
    private static DemoHandlerFactory demoHandlerFactory;

    public static String bizUserId = "leegean";

    public static String accountSetNo = "12985739202038";

    private static String setPayPwdUrl = "http://116.228.64.55:9092/pwd/setPayPwd.html";

    public static void main(String[] args) {
        try {
            //初始化
            init();

            //服务对象
            String serviceName = "MemberService";
            //调用方法
            String methodName = "createMember";          //创建会员
//            String methodName = "sendVerificationCode";  //发送短信验证码
//            String methodName = "bindPhone";             //绑定手机
//            String methodName = "changeBindPhone";       //重置绑定手机（短信验证码确认）
//            String methodName = "setRealName";           //个人实名认证
//            String methodName = "setCompanyInfo";        //设置企业信息
//            String methodName = "getMemberInfo";         //获取会员信息
//            String methodName = "getBankCardBin";        //查询卡bin
//            String methodName = "applyBindBankCard";     //请求绑定银行卡
//            String methodName = "bindBankCard";          //确认绑定银行卡
//            String methodName = "setSafeCard";           //设置安全卡
//            String methodName = "queryBankCard";         //查询绑定银行卡
//            String methodName = "unbindBankCard";        //解绑绑定银行卡
//            String methodName = "lockMember";            //锁定会员
//            String methodName = "unlockMember";          //解锁会员

//            String serviceName = "OrderService";
//            String methodName = "depositApply";          //充值申请
//            String methodName = "withdrawApply";         //提现申请
//            String methodName = "consumeApply";          //消费申请
//            String methodName = "agentCollectApplySimplify"; //托管代收申请（简化版）
//            String methodName = "signalAgentPaySimplify";    //单笔托管代付（简化版）
//            String methodName = "batchAgentPaySimplify";     //批量托管代付（简化版）
//            String methodName = "pay";                   //确认支付（后台+短信验证码确认）
//            String methodName = "entryGoods";            //商品录入
//            String methodName = "queryModifyGoods";      //查询、修改商品
//            String methodName = "freezeMoney";           //冻结金额
//            String methodName = "unfreezeMoney";         //解冻金额
//            String methodName = "refund";                //退款申请
//            String methodName = "failureBidRefund";      //流标专用退款
//            String methodName = "applicationTransfer";   //平台转账
//            String methodName = "queryBalance";          //查询余额
//            String methodName = "getOrderDetail";        //查询订单状态
//            String methodName = "queryInExpDetail";      //查询收支明细

//            String serviceName = "MerchantService";
//            String methodName = "queryReserveFundBalance"; //通联通头寸查询
//            String methodName = "getCheckAccountFile";   //平台集合对账下载
//            String methodName = "queryMerchantBalance";

            IDemoHandler handler = demoHandlerFactory.getHandler(serviceName, methodName);
            JSONObject params = handler.getRequestParams();
            new JSONObject(new Object());
            System.out.println("请求参数：" + params.toString());
            JSONObject response = client.request(serviceName, methodName, handler.getRequestParams());

            System.out.println("返回数据：" + response.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void setPayPwd() throws IOException {
        CloseableHttpClient httpClient = HttpClients.createSystem();
        HttpGet httpGet = new HttpGet(setPayPwdUrl);
        String body = httpClient.execute(httpGet, response -> {

            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity, "UTF-8");
        });

    }

    private static void init() {
        URL resource = DemoPortal.class.getClassLoader().getResource(path);
        path = resource.getPath();
        try {
            publicKey = RSAUtil.loadPublicKey(alias, path, pwd);
            privateKey = RSAUtil.loadPrivateKey(alias, path, pwd);
            client = new SOAClient();
            client.setServerAddress(serverAddress);
            client.setSignKey(privateKey);
            client.setPublicKey(publicKey);
            client.setSysId(sysid);
            client.setSignMethod(signMethod);

            demoHandlerFactory = new DemoHandlerFactory();

            DemoUtils.setParams(publicKey, privateKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
